﻿Imports System.Data
Imports System.Data.SqlClient
Partial Class ntinhthanh
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            BindGrid()
        End If

    End Sub

    Public Sub Datagrid_OnCancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        DataGrid1.EditItemIndex = -1
        BindGrid()
    End Sub
    Public Sub Datagrid_OnEdit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        DataGrid1.EditItemIndex = e.Item.ItemIndex
        BindGrid()
    End Sub
    Public Sub Datagrid_OnUpdate(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        Dim sSql As String
        Dim blnAddNew As Boolean = False
        Dim txtTentinhthanhV As TextBox
        Dim txttentinhthanhE As TextBox
        Dim txttentinhthanhF As TextBox
        Dim txttentinhthanhG As TextBox
        Dim txttentinhthanhC As TextBox
        Dim txttentinhthanhS As TextBox

        Dim txtTT As TextBox
        Dim txtThutu As TextBox

        '----------------Lay du lieu -------------

        txtTentinhthanhV = e.Item.Cells(2).Controls(0)
        txttentinhthanhE = e.Item.Cells(3).Controls(0)
        txttentinhthanhF = e.Item.Cells(4).Controls(0)
        txttentinhthanhG = e.Item.Cells(5).Controls(0)
        txttentinhthanhC = e.Item.Cells(6).Controls(0)
        txttentinhthanhS = e.Item.Cells(7).Controls(0)

        txtTT = e.Item.Cells(8).Controls(0)
        txtThutu = e.Item.Cells(9).Controls(0)
        '----------------Update du lieu -------------
        If e.Item.Cells(1).Text < 1 Then
            blnAddNew = True
        End If
        If blnAddNew = True Then
            ' Insert into
            sSql = " insert into tinhthanh (tentinhthanhV,tentinhthanhE,tentinhthanhF,tentinhthanhG,tentinhthanhC,tentinhthanhS,TT,thutu)" & _
            "values(@tentinhthanhV,@tentinhthanhE,@tentinhthanhF,@tentinhthanhG,@tentinhthanhC,@tentinhthanhS,@TT,@thutu) "
        Else
            'Update
            sSql = " UPDATE tinhthanh SET" & _
            "  tentinhthanhV =@tentinhthanhV" & _
            " ,tentinhthanhE =@tentinhthanhE" & _
            " ,tentinhthanhF =@tentinhthanhF" & _
            " ,tentinhthanhG =@tentinhthanhG" & _
            " ,tentinhthanhC =@tentinhthanhC" & _
            " ,tentinhthanhS =@tentinhthanhS" & _
            ", TT =@TT, thutu=@thutu" & _
            " WHERE idtinhthanh = @idtinhthanh"
        End If
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        If Not blnAddNew Then
            MyCommand.Parameters.Add(New SqlParameter("@idtinhthanh", e.Item.Cells(1).Text))
        End If
        MyCommand.Parameters.Add(New SqlParameter("@tentinhthanhV", txtTentinhthanhV.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tentinhthanhE", txttentinhthanhE.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tentinhthanhF", txttentinhthanhF.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tentinhthanhG", txttentinhthanhG.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tentinhthanhC", txttentinhthanhC.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tentinhthanhS", txttentinhthanhS.Text))

        MyCommand.Parameters.Add(New SqlParameter("@thutu", CInt(txtThutu.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@TT", IIf(txtTT.Text = "", 1, CBool(txtTT.Text))))
        MyCommand.ExecuteNonQuery()
        myConn.Close()
        DataGrid1.EditItemIndex = -1
        BindGrid()
    End Sub
    Private Sub BindGrid()
        Dim dr As DataRow
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim sSql As String = "select *  from tinhthanh  order by nuocngoai desc,tentinhthanhV, thutu, idtinhthanh"
        myConn.Open()
        Dim myAdapter As New SqlDataAdapter(sSql, myConn)
        Dim myDataSet As New DataSet
        myAdapter.Fill(myDataSet, sSql)
        dr = myDataSet.Tables(0).NewRow()
        dr(0) = -1
        dr(1) = "Thêm mới"
        myDataSet.Tables(0).Rows.InsertAt(dr, 0)
        DataGrid1.DataSource = myDataSet
        DataGrid1.DataBind()
        myConn.Close()
    End Sub

    Public Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        BindGrid()
    End Sub
End Class
